﻿using System;
using System.Collections.Generic;
using System.Text;

namespace list
{
    class mylist<T>
    {
        public int capicity
        {
            get; set;

        }

        public int count//count是下一个将要插入的数字的下标
        {
            get;set;
        }

        private T[] arr;

        private void init()
        {
            capicity = 4;
            count = 0;
        }

        private void Capicity()
        {
            T []temp = new T[capicity*2];
            for(int i=0;i<capicity;i++)
            {
                temp[i] = arr[i];
            }
            arr = temp;
            capicity *= 2;
           
        }

        public mylist(int capicity)
        {
            init();
            this.capicity = capicity;
            arr = new T[capicity];
            
        }

        public mylist()
        {
            init();
            arr = new T[capicity];

        }

        public void Add(T num)
        {
            if(count>=capicity)
            {
                Capicity();

            }
            arr[count] = num;
            count++;
        }

        public void Insert(int pos,T date)
        {
            if(count>=capicity)
            {
                Capicity();
            }
            for(int i=count;i>=pos;i--)
            {
                arr[i] = arr[i - 1];
            }
            arr[pos - 1] = date;



        }

        public void Modifiy(int pos,T date)
        {
            if(pos<=count)
            {
                arr[pos - 1] = date;

;           }
            else
            {
                Console.WriteLine("超出范围");
            }
        }

        public void Show()
        {
            for(int i=0;i<count;i++)
            {
                Console.WriteLine(arr[i]);
            }
        }

        public T this[int i]
        {
            get
            {
                if (i < 0 || i >= count)
                {
                    throw new System.ArgumentOutOfRangeException("索引参数超出范围");
                }
                return arr[i];

            }
            set
            {
                if (i < 0 || i >= count)
                {
                    throw new System.ArgumentOutOfRangeException("索引参数超出范围");
                }
                arr[i] = value;
            }
        }
        public int Indexof(T date)
        {
            for(int i=0;i<count;i++)
            {
                if(date.Equals(arr[i]))
                {
                    return i;

                }
            }

            return -1;
        }
            
            

    }
}
